/* $Id$ */

function tweakPODHtml(pod){
    var newPod = $(pod);//.find('body');
    console.log(newPod);
    
};

$(function() {

    $("#dataTable").dataTable({"bJQueryUI": true,
		                       "sPaginationType": "full_numbers"
                              }
                             );

    $("#book_select").change(function() {
        console.log("Changed book select to " + $(this).val());
    });

    $.getJSON( '/tree/' + $(this).val() + "/", 
               { rm: 'get_tree' },
               function(data) {
                   $('#file_tree').tree(
                       {
                           data: data,
                           selectable: true,
                           saveState: true,
                           onCreateLi : function(node, $li) {
                               //console.log("xxx", $($li).text());
                               //console.log("yyy", node);

                               console.log("Namespace: ", node.id);
                               var url = 'cgiman.cgi';
                               url += '?rm=pod';
                               //url += '&full_path=/home/eberg/work/CGIMan/lib/perl5/';
                               url += node.id;

                               /* */
                               $($li).find('.jqtree-title,')
                                   .filter(':contains(".pm")')
                                   .attr('id', node.id)
                                   .addClass("leaf");
                           }
                       });
               });

    $("#file_group_select").append('<option value="">-- Select a file group -- </option>')
        .val('');

    /* add the click event on the file names to podify them into the right div */
    $(".leaf").live( "click", function(){
        console.log("Hello!");
        console.log($(this).attr('id'));
        jQuery.get('cgiman.cgi', { rm: "pod",
                                   full_path: $(this).attr('id')
                                 },
                   function(data){
                       //alert("data load");
                       var container = $( '<div id="pod_container" />');
                       //console.log("data: " + data);
                       var newPod = $(data).remove('title').remove('meta');
                       //console.log("NewPod: ", newPod);
                       
                       $(container).html( newPod );
                       tweakPODHtml( $(container) );

                       $("#render_pane").html(container);
                   },
                   'html'
                  );
    }
                    );
        
});